package cn.com.shopec.erp.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;

public class URLController 
{
	
	private static Logger logger = Logger.getLogger(URLController.class);
	
	public static String getData(String httpUrl,String pageSize,String currentPage,
			String startTime,String endTime,String methodName,String requestForm ,String orderStatus,String shipStatus,String payStatus) throws Exception
	{
		
		InputStream inputStream = null;
		BufferedReader bufferedReader = null;
		HttpClient httpClient = new HttpClient();
		PostMethod post = new PostMethod(httpUrl);
		post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF8"); 
		
		// 使用POST方法
		NameValuePair method = new NameValuePair("method", methodName);
		NameValuePair format = new NameValuePair("format", "json");
		NameValuePair request_from = new NameValuePair("request_from", requestForm);
		NameValuePair start_time = new NameValuePair("start_time", startTime);
		NameValuePair end_time = new NameValuePair("end_time", endTime);
		NameValuePair order_status = new NameValuePair("order_status", null==orderStatus?"":orderStatus);
		NameValuePair ship_status = new NameValuePair("ship_status", null==shipStatus?"":shipStatus);
		NameValuePair pay_status = new NameValuePair("pay_status", null==payStatus?"":payStatus);
		NameValuePair page_size = new NameValuePair("page_size", pageSize);
		NameValuePair current_page = new NameValuePair("current_page", currentPage);
		NameValuePair app_id = new NameValuePair("app_id", "ecos.ome");
		NameValuePair nosign = new NameValuePair("nosign", "1");
		post.setRequestBody(new NameValuePair[] { method, format, request_from,start_time,end_time,order_status,ship_status,pay_status,page_size,current_page,app_id,nosign});
		
		try
		{
			httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(8000);
			httpClient.executeMethod(post);
			inputStream = post.getResponseBodyAsStream();  
			bufferedReader = new BufferedReader(new InputStreamReader(inputStream));  
	        StringBuffer resBuffer = new StringBuffer();  
	        String resTemp = "";  
	        
	        while((resTemp = bufferedReader.readLine()) != null)
	        {  
	            resBuffer.append(resTemp);  
	        }  
	        
	        return resBuffer.toString();
		}
		catch (SocketTimeoutException e) 
		{
			logger.error("getData SocketTimeoutException order: time out "+e.getStackTrace());
			throw e;
		}
		catch(Exception e)
		{
			logger.error("getData error:"+e.getStackTrace());
			throw e;
		}
		finally
		{
			post.releaseConnection();
			if(bufferedReader != null)
			{
				try 
				{
					bufferedReader.close();
				} 
				catch (IOException e) 
				{
					logger.error("getData bufferedReader error:"+e.getStackTrace());
				}
			}
			
			if(inputStream != null)
			{
				try 
				{
					inputStream.close();
				} 
				catch (IOException e) 
				{
					logger.error("getData inputStream error:"+e.getStackTrace());
				}
			}
		}
		
	}
	
}
